package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.Reagent;
import com.biovr.backend.repository.ReagentRepository;
import com.biovr.backend.service.ReagentServiceImpl;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by monodev on 2017/5/8.
 */
@Api(value = "/reagents",description = "试剂试剂", consumes="application/json")
@RestController
@RequestMapping("/reagents")
public class ReagentRestController {
    @Autowired
    private ReagentRepository reagentRepository;
    @Autowired
    private ReagentServiceImpl reagentService;

    /**
     * 获得所有试剂
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN','SUPER')")
    @ApiOperation(value="获得status=active试剂列表",response = Reagent.class,responseContainer = "List")
    @GetMapping
    public RestResponse<List<Reagent>> getAll(){
        List<Reagent> allReagents =reagentRepository.findAll();
        return  ResponseFactory.createRestResponse(true,"",allReagents);
    }

    /**
     * 获得所有active试剂
     * @return
     */
    @ApiOperation(value="获得status=active试剂列表",response = Reagent.class,responseContainer = "List")
    @GetMapping("/actived")
    public RestResponse<List<Reagent>> getAllActived(){
        List<Reagent> allReagents =reagentRepository.findByStatus(true);
        return  ResponseFactory.createRestResponse(true,"",allReagents);
    }

    /**
     * 查找试剂
     * @param id
     * @return
     */
    @ApiOperation(value="根据ID查找试剂",response = Reagent.class)
    @GetMapping("/{id}")
    public RestResponse<Reagent> findOne(@PathVariable String id){
        Reagent tarReagent = reagentRepository.findOne(id);
        return ResponseFactory.createRestResponse(true,"",tarReagent);
    }

    /**
     * 添加新试剂
     * @param request
     * @param reagent
     * @return
     */
    @ApiOperation(value="增加试剂", notes="使用json格式POST添加新的试剂",response = Reagent.class,responseContainer = "List")
    @ApiImplicitParam(name = "reagent", value = "新增试剂对象，至少包括试剂名称", required = true, dataType = "Reagent")
    @PostMapping
    public  RestResponse<?> addReagent(HttpServletRequest request, @RequestBody Reagent reagent) throws  Exception{
        //检查名称
        if(null==reagent.getName()||reagent.getName().equals("")){
            return ResponseFactory.createRestResponse(false,"试剂名称不能为空！",null);
        }
        Reagent addedReag = reagentService.addReagent(request,reagent);
        return addedReag==null?
                ResponseFactory.createRestResponse(false,"无法添加试剂",null)
                : ResponseFactory.createRestResponse(true,"添加试剂成功",addedReag);
    }

    /**
     * 修改实验试剂
     * @param reagent
     * @return
     * @throws Exception
     */
    @PutMapping
    public RestResponse<Reagent> updateReagent(@RequestBody Reagent reagent) throws Exception{
        Reagent target;
        //检查名称
        if(null==reagent.getName()||reagent.getName().equals("")){
            return ResponseFactory.createRestResponse(false,"试剂名称不能为空！",null);
        }
        //检查标准编号
        if(null!=reagent.getStdCode() && reagent.getStdCode()!=""){
            target = reagentRepository.findByStdCode(reagent.getStdCode());
            if(target!=null&&!target.getId().equals(reagent.getId())){
                return ResponseFactory.createRestResponse(false,"试剂标准编号重复！",null);
            }
        }
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        reagent.setUpdateTime(sdf.format(new Date()));
        Reagent updatedExpm = reagentRepository.save(reagent);
        return ResponseFactory.createRestResponse(true,"修改试剂成功",updatedExpm);
    }
    @ApiOperation(value="重新生成二维码")
    @ApiImplicitParam(name = "id", value = "实验id", required = true, dataType = "String",paramType = "path")
    @PostMapping("/qrcode/{id}")
    public  RestResponse<String> regenerateQrcode(HttpServletRequest request,@PathVariable String id) throws Exception{
        return ResponseFactory.createSuccessResult( reagentService.regenerateQrcode(request,id));
    }
    /**
     * 根据ID删除试剂
     * @param id
     * @return
     */
    @ApiOperation(value="DELETE根据ID删除试剂", notes="使用试剂ID删除试剂")
    @ApiImplicitParam(name = "id", value = "删除试剂Id", required = true, dataType = "string",paramType = "path")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public RestResponse<?> deleteExpirement(@PathVariable String id) throws Exception{
        reagentService.deleteReagent(id);
        return  ResponseFactory.createSuccessResult();
    }

    /**
     * 根据试剂标准名称查找
     * @param stdCode
     * @return
     */
    @ApiOperation(value="根据试剂标准名称查找")
    @ApiImplicitParam(name = "stdCode", value = "试剂标准名称", required = true, dataType = "String",paramType = "path")
    public RestResponse<Reagent> getByStdCode(@PathVariable String stdCode){
        return ResponseFactory.createSuccessResult(reagentRepository.findByStdCode(stdCode));
    }
}
